package cn.chinaflame.pagodas.web;

import cn.chinaflame.pagodas.Result.Result;
import cn.chinaflame.pagodas.annotation.ApiDoc;
import cn.chinaflame.pagodas.entity.PaMember;
import cn.chinaflame.pagodas.enums.CodeEnum;
import cn.chinaflame.pagodas.repository.PaMemberRepository;
import cn.chinaflame.pagodas.req.RegistReq;
import cn.chinaflame.pagodas.req.WebRegistDetailReq;
import cn.chinaflame.pagodas.service.PaMemberService;
import cn.chinaflame.pagodas.service.PaOrderService;
import cn.chinaflame.pagodas.utils.ParamPreconditions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import cn.chinaflame.pagodas.Result.Result;
import cn.chinaflame.pagodas.entity.Boy;
import cn.chinaflame.pagodas.repository.BoyRepository;
import cn.chinaflame.pagodas.service.BoyService;
import cn.chinaflame.pagodas.utils.ResultUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;
/**
 * 佛龛功德榜接口
 *
 * @author admin
 */
@CrossOrigin
@RestController
@Transactional(rollbackFor = Exception.class)
@RequestMapping(value = "/openapi/v1/pagodas/web/")
public class OpenWebAPI {

    private static final Logger log = LoggerFactory.getLogger(OpenWebAPI.class);

    @Autowired
    private PaOrderService paOrderService;

    @Autowired
    private PaMemberService paMemberService;

    @Autowired
    private PaMemberRepository paMemberRepository;

    /**
     * 请供金额接口
     * 功德榜接口
     *
     * @return
     */
    @GetMapping(value = "query/list")
    @ApiDoc(Result.class)
    public Object merit() {

        return Result.create(paOrderService.merits());
    }

    /**
     * 功德簿供灯数量接口
     *
     * @return
     */
    @GetMapping(value = "query/number")
    @ApiDoc(Result.class)
    public Object numberOfLines() {
        return Result.create(paOrderService.numberOfLines());
    }

    /**
     * 功德簿供灯天数接口
     *
     * @return
     */
    @GetMapping(value = "query/trem")
    @ApiDoc(Result.class)
    public Object tremOfLines() {
        return Result.create(paOrderService.tremOfLines());
    }

    /**
     * 功德簿分享次数接口
     *
     * @return
     */
    @GetMapping(value = "query/share")
    @ApiDoc(Result.class)
    public Object shareOfLines() {
        return Result.create(paOrderService.shareOfLines());
    }

    /**
     * 第一步邀请好友
     *
     * @param openId 微信openid
     * @return
     */
    @PostMapping(value = "invid")
    @ApiDoc(Result.class)
    public Object invid(String openId) {
        ParamPreconditions.checkNotEmpty(openId, CodeEnum.FAIL.getCode(), "openid为空");

        return null;
    }

    /**
     * 注册接口绑定微信公众号接口
     *
     * @param req
     * @return
     */
    @PostMapping(value = "register")
    @ApiDoc(RegistReq.class)
    public Object register(@RequestBody RegistReq req) {
        log.info("微信绑定手机号req:{}", req);
        req.check();
        //ParamPreconditions.checkNotEmpty(sms, CodeEnum.FAIL.getCode(), "验证码不能为空");
        return Result.create(paMemberService.add(req.getMobile(), req.getOpenid()));
    }

    /**
     * 注册接口绑定微信公众号接口
     *
     * @param req
     * @return
     */
    @PostMapping(value = "register/detail")
    @ApiDoc(WebRegistDetailReq.class)
    public Object registerDetail(@RequestBody WebRegistDetailReq req) {
        log.info("保存微信详细信息req:{}", req);
        req.check();
        return paMemberService.updateNickDetail(req);
    }

    /**
     * 注册接口绑定微信公众号接口
     *
     * @param openid   openid
     * @param mobile   手机号
     * @param memberId 会员id
     * @return
     */
    @GetMapping(value = "query/detail")
    @ApiDoc(Result.class)
    public Object queryDetail(String openid, String mobile, String memberId) {
        log.info("查询微信详情openid:{},mobile:{},memberId:{}", openid, mobile, memberId);
        return paMemberService.queryNickDetail(openid, mobile, memberId);
    }

    /**
     *
     * 根据openid查询手机号
     *
     * @param openid openid
     * @return
     */
    @GetMapping(value = "getMobile")
    @ApiDoc(Result.class)
    public Object getMobileByOpenId(String openid) {
        log.info("根据openid查询手机号openid:{}", openid);
        ParamPreconditions.checkNotEmpty(openid, CodeEnum.FAIL.getCode(), "openid为空");
        PaMember paMember = paMemberRepository.findPaMemberByOpenid(openid);
        ParamPreconditions.checkArgument(paMember != null, CodeEnum.FAIL.getCode(), "没有这个会员");
        String mobile = paMember.getMobile();
        ParamPreconditions.checkNotEmpty(mobile, CodeEnum.FAIL.getCode(), "你还未绑定手机号");
        return Result.create(mobile);
    }


    /**
     * 我的祝福接口
     *//*
	@GetMapping(value="bless")
	public Object bless(){
		//paBlessService.bless();
		return paBlessService.bless();
	}*/
}






